package gui;

import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import java.awt.Dimension;
import javax.swing.JPanel;

import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

import javax.swing.JButton;

import control.Configuracion;

import java.awt.Insets;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import javax.swing.JList;

@SuppressWarnings("serial")
public class InterfazProbadores extends JDialog {
	private JPanel jContentPane = null;
	private JScrollPane jSPLista = null;
	private JList jLNoSeleccionados = null;
	private JPanel jPBotones = null;
	private JButton jBCancelar = null;
	private JButton jBGenerar = null;

	private DefaultListModel seleccionados = null;
	private DefaultListModel noSeleccionados = null;

	private JScrollPane jScrollPane = null;
	private JList jLSeleccionados = null;
	private JButton jBSeleccionar = null;
	private JButton jBDeseleccionar = null;

	private List<String> probadores = null;
	
	private boolean cancel = true;
	
	/**
	 * This method initializes
	 * 
	 */
	public InterfazProbadores(JFrame frame, List<String> probadores) {
		super();
		this.probadores = probadores;
		initialize(frame);
		cargarProbadores();
		setVisible(true);
	}

	@SuppressWarnings("unchecked")
	private void cargarProbadores() {	
		List todosProbadores = Configuracion.getList(Configuracion.USUARIOSPROBADORES);
		if (todosProbadores != null) {
			Iterator i = todosProbadores.iterator();
			while (i.hasNext()) {
				String probador = (String)i.next();
				if (probadores.contains(probador))
					seleccionados.addElement(probador);
				else
					noSeleccionados.addElement(probador);
			}
		}
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize(JFrame frame) {
		this.setSize(new Dimension(596, 267));
		this.setTitle("Generar informe de notas");
		this.setModal(true);
		this.setContentPane(getJContentPane());
		this.setLocationRelativeTo(frame);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 2;
			gridBagConstraints6.weighty = 1.0;
			gridBagConstraints6.anchor = GridBagConstraints.NORTH;
			gridBagConstraints6.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints6.gridy = 1;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 2;
			gridBagConstraints5.weighty = 1.0;
			gridBagConstraints5.anchor = GridBagConstraints.SOUTH;
			gridBagConstraints5.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints5.gridy = 0;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = GridBagConstraints.BOTH;
			gridBagConstraints4.gridy = 0;
			gridBagConstraints4.weightx = 1.0;
			gridBagConstraints4.weighty = 1.0;
			gridBagConstraints4.gridheight = 2;
			gridBagConstraints4.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints4.gridx = 3;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 2;
			gridBagConstraints.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints.fill = GridBagConstraints.BOTH;
			gridBagConstraints.gridwidth = 2;
			gridBagConstraints.gridy = 0;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.fill = GridBagConstraints.BOTH;
			gridBagConstraints11.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints11.gridwidth = 4;
			gridBagConstraints11.gridy = 5;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.BOTH;
			gridBagConstraints3.gridy = 0;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints3.weighty = 1.0;
			gridBagConstraints3.gridheight = 2;
			gridBagConstraints3.gridx = 1;
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
			jContentPane.add(getJSPLista(), gridBagConstraints3);
			jContentPane.add(getJPBotones(), gridBagConstraints11);
			jContentPane.add(getJScrollPane(), gridBagConstraints4);
			jContentPane.add(getJBSeleccionar(), gridBagConstraints5);
			jContentPane.add(getJBDeseleccionar(), gridBagConstraints6);
		}
		return jContentPane;
	}

	private JScrollPane getJSPLista() {
		if (jSPLista == null) {
			jSPLista = new JScrollPane();
			jSPLista.getViewport().add(getJLNoSeleccionados());
		}
		return jSPLista;
	}

	/**
	 * This method initializes jCBIncidencias
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JList getJLNoSeleccionados() {
		if (jLNoSeleccionados == null) {
			noSeleccionados = new DefaultListModel();
			jLNoSeleccionados = new JList(noSeleccionados);
			jLNoSeleccionados.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		}
		return jLNoSeleccionados;
	}

	/**
	 * This method initializes jPBotones
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPBotones() {
		if (jPBotones == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setHgap(10);
			flowLayout.setVgap(5);
			jPBotones = new JPanel();
			jPBotones.setLayout(flowLayout);
			jPBotones.add(getJBGenerar(), null);
			jPBotones.add(getJBCancelar(), null);
		}
		return jPBotones;
	}

	/**
	 * This method initializes jBCancelar
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJBCancelar() {
		if (jBCancelar == null) {
			jBCancelar = new JButton();
			jBCancelar.setText("Cancelar");
			jBCancelar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setVisible(false);
				}
			});
		}
		return jBCancelar;
	}

	/**
	 * This method initializes jBGenerar
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJBGenerar() {
		if (jBGenerar == null) {
			jBGenerar = new JButton();
			jBGenerar.setText("Aceptar");
			jBGenerar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					cancel = false;
					setVisible(false);
				}
			});
		}
		return jBGenerar;
	}

	@SuppressWarnings("unchecked")
	public List<String> probadores() {
		List<String> l = new ArrayList<String>();
		for (Enumeration e = seleccionados.elements(); e.hasMoreElements();)
			l.add((String) e.nextElement());
		return l;
	}

	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJLSeleccionados());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jLSeleccionados
	 * 
	 * @return javax.swing.JList
	 */
	private JList getJLSeleccionados() {
		if (jLSeleccionados == null) {
			seleccionados = new DefaultListModel();
			jLSeleccionados = new JList(seleccionados);
			jLSeleccionados.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		}
		return jLSeleccionados;
	}

	/**
	 * This method initializes jBSeleccionar
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJBSeleccionar() {
		if (jBSeleccionar == null) {
			jBSeleccionar = new JButton();
			jBSeleccionar.setText(">");
			jBSeleccionar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					int[] indices = getJLNoSeleccionados().getSelectedIndices();
					if (indices.length > 0) {
						for (int i = 0; i < indices.length; i++) {
							seleccionados.addElement(noSeleccionados.get(indices[i]));
						}
						Object [] values = getJLNoSeleccionados().getSelectedValues();
						for (int i = 0; i < values.length; i++) {
							noSeleccionados.removeElement(values[i]);
						}
					}
				}
			});
		}
		return jBSeleccionar;
	}

	/**
	 * This method initializes jBDeseleccionar
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJBDeseleccionar() {
		if (jBDeseleccionar == null) {
			jBDeseleccionar = new JButton();
			jBDeseleccionar.setText("<");
			jBDeseleccionar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					int[] indices = getJLSeleccionados().getSelectedIndices();
					if (indices.length > 0) {
						for (int i = 0; i < indices.length; i++) {
							noSeleccionados.addElement(seleccionados.get(indices[i]));
						}
						Object [] values = getJLSeleccionados().getSelectedValues();
						for (int i = 0; i < values.length; i++) {
							seleccionados.removeElement(values[i]);
						}
					}
				}
			});
		}
		return jBDeseleccionar;
	}
	
	public boolean canceled(){
		return cancel;
	}
} // @jve:decl-index=0:visual-constraint="85,62"